<?php
/**
 * Created by JetBrains PhpStorm.
 * User: YangZi
 * Date: 11-6-20
 * Time: 下午4:17
 */
 
class BaoBeiAlbumModel extends Model{

    public function get($album_id){

        $sql = "select * from baobeialbum where album_id = :album_id";

        return $this->db()->getRow($sql,array('album_id'=>$album_id));
    }

    public function getBaoBeiAlbumList($curr_page,$per_page,$filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['album_name'])){
            $where .= ' and album_name like :album_name';
            $bindarr['album_name'] = '%'.$filter['album_name'].'%';
        }

        if(isset($filter['album_status'])){
            $where .= ' and album_status = :album_status';
            $bindarr['album_status'] = $filter['album_status'];
        }

        $ret = $this->pager($curr_page, $per_page)
                ->ct('select count(*) from baobeialbum where ' . $where , $bindarr)
                ->rs('select * from baobeialbum where '.$where.' order by album_id asc',$bindarr);
        return $ret;
    }
    
    public function addBaoBeiAlbum($album_name,$album_content,$album_status,$album_status_content,$user_id){

        $sql = "insert into baobeialbum(album_name,album_content,album_status,album_status_content,user_id,add_time) values(:album_name,:album_content,:album_status,:album_status_content,:user_id,now())";

        $bindarr = array(
            'album_name'=>$album_name,
            'album_content'=>$album_content,
            'album_status'=>$album_status,
            'album_status_content'=>$album_status_content,
            'user_id' => $user_id
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    public function updateBaoBeiAlbum($id,$album_name,$album_content,$album_status,$album_status_content,$user_id){

        $sql = "update baobeialbum set album_name = :album_name,album_content = :album_content,album_status = :album_status,album_status_content = :album_status_content where id = :id and user_id = :user_id";

        $bindarr = array(
            'id'=>$id,
            'user_id' => $user_id,
            'album_name'=>$album_name,
            'album_content'=>$album_content,
            'album_status'=>$album_status,
            'album_status_content'=>$album_status_content
        );

        return $this->db()->execute($sql,$bindarr);
    }

    public function delUser($album_id){

        $sql = "delete from baobeialbum where album_id = :album_id";

        $bindarr = array(
            'album_id'=>$album_id,
        );
        $rs = $this->db()->execute($sql,$bindarr);
        return $rs->rowCount();
    }

    public function getAllAlbumList(){
        $sql = "select * from baobeiAlbum";
        return $this->db()->getAll($sql);
    }
}